package cn.iocoder.yudao.module.hs.controller.admin.qcorderdetail;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.hs.controller.admin.qcorderdetail.vo.*;
import cn.iocoder.yudao.module.hs.dal.dataobject.qcorderdetail.QcOrderDetailDO;
import cn.iocoder.yudao.module.hs.service.qcorderdetail.QcOrderDetailService;

@Tag(name = "管理后台 - 质检单明细")
@RestController
@RequestMapping("/hs/qc-order-detail")
@Validated
public class QcOrderDetailController {

    @Resource
    private QcOrderDetailService qcOrderDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建质检单明细")
    @PreAuthorize("@ss.hasPermission('hs:qc-order-detail:create')")
    public CommonResult<Long> createQcOrderDetail(@Valid @RequestBody QcOrderDetailSaveReqVO createReqVO) {
        return success(qcOrderDetailService.createQcOrderDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新质检单明细")
    @PreAuthorize("@ss.hasPermission('hs:qc-order-detail:update')")
    public CommonResult<Boolean> updateQcOrderDetail(@Valid @RequestBody QcOrderDetailSaveReqVO updateReqVO) {
        qcOrderDetailService.updateQcOrderDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除质检单明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:qc-order-detail:delete')")
    public CommonResult<Boolean> deleteQcOrderDetail(@RequestParam("id") Long id) {
        qcOrderDetailService.deleteQcOrderDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得质检单明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:qc-order-detail:query')")
    public CommonResult<QcOrderDetailRespVO> getQcOrderDetail(@RequestParam("id") Long id) {
        QcOrderDetailDO qcOrderDetail = qcOrderDetailService.getQcOrderDetail(id);
        return success(BeanUtils.toBean(qcOrderDetail, QcOrderDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得质检单明细分页")
    @PreAuthorize("@ss.hasPermission('hs:qc-order-detail:query')")
    public CommonResult<PageResult<QcOrderDetailRespVO>> getQcOrderDetailPage(@Valid QcOrderDetailPageReqVO pageReqVO) {
        PageResult<QcOrderDetailDO> pageResult = qcOrderDetailService.getQcOrderDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QcOrderDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出质检单明细 Excel")
    @PreAuthorize("@ss.hasPermission('hs:qc-order-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQcOrderDetailExcel(@Valid QcOrderDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QcOrderDetailDO> list = qcOrderDetailService.getQcOrderDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "质检单明细.xls", "数据", QcOrderDetailRespVO.class,
                        BeanUtils.toBean(list, QcOrderDetailRespVO.class));
    }

}